package linuxbox.download.bean;

import java.util.*;

import linuxbox.BoxStatic;
import linuxbox.exception.*;
import linuxbox.bean.*;
import linuxbox.util.*;
import linuxbox.download.logic.DownLoadObject;

public class DownLoadBean extends DownLoadObject implements ITableBean {
	protected DbAdapter m_db = null;
	static protected String m_TableName = "download_file";
  
	public DownLoadBean(DownLoadKey pk, DbAdapter db) {
		this.init(pk, db);
	}

	public DownLoadBean(DownLoadKey pk) {
		this.init(pk, null);
	}

	public DownLoadBean() {
		this.init(null, null);
	}

	public void init(DownLoadKey pk, DbAdapter db) {
		if (db != null) {
			m_db = db;
		}
		
		if (pk != null)
			m_iID = pk.getID();
	}

	public String getTableName() {
		return m_TableName;
	}
	
	public void setTableName(String str) {
		m_TableName = str;
	}

	public void makeConnection() throws Exception {
		if ( m_db == null )
			m_db = new DbAdapter(true);
	}
	
	public void closeConnection() throws Exception {
		if (m_db != null)
			m_db.close();
	}

	public void load() throws Exception {
		String sql;

		sql = "select f.id, f.name, f.cat_id, c.name, f.file_name, "
			+ "f.description, f.file_size, f.upload_date from download_file f, "
			+ "download_cat c where f.id = " + m_iID + " and c.id=f.cat_id";
    
	    makeConnection();
		m_db.executeQuery(sql);

		if (m_db.next()) {
			m_iID = m_db.getInt(1);
			m_sName = m_db.getString(2);
			m_iCatID = m_db.getInt(3);
			m_sCatName = m_db.getString(4);
			m_sFileName = m_db.getString(5);
			m_sDesc = m_db.getString(6);
			m_iFileSize = m_db.getInt(7);
			//String tmpStr = m_db.getString(8);
			//BoxStatic.logger.debug(tmpStr);
			//m_tsUploadDate = Timestamp.valueOf(tmpStr.substring(0, 18));
			m_tsUploadDate = m_db.getTimestamp(8);
		} else {
			closeConnection();
			throw new LinuxboxException(ExceptionHelper.DOWNLOAD, "DL0001", "Load error:  m_iID not found!");
		}
		
		closeConnection();
	}

	public void store() throws Exception {
		String dtStr = BoxStatic.formater.postgresTimestamp(m_tsUploadDate);
		        
		String sql = "UPDATE " + getTableName() + " set"
			+ " name = '" + m_sName + "',"
		    + " cat_id = " + m_iCatID + ","
		    + " file_name = '" + m_sFileName + "',"
		    + " description = '" + m_sDesc + "',"
		    + " file_size = " + m_iFileSize + ","
		    + " upload_date = '" + dtStr + "'"
		    + " where id = " + m_iID;
		             
		makeConnection();
		m_db.executeUpdate(sql);
		closeConnection();
	}

	public void refresh() throws Exception {
		load();
	}

	public void create() throws Exception {
	
	}

	public void create(DownLoadObject obj) throws Exception {
		String dtStr = BoxStatic.formater.postgresTimestamp(obj.getUploadDate());

		String sql = null;
		makeConnection();
		
		int id = obj.getID();
		
		if (id == -1) {
			sql = "select max(id) from download_file";
			m_db.executeQuery(sql);

			if (m_db.next()) {
				id = m_db.getInt(1) + 1;
			}
		}
		
		sql = "insert into download_file(id, name, cat_id, file_name, "
			+ "description, file_size, upload_date) values(" 
			+ id + ", '" + obj.getName() + "', "
			+ obj.getCatID() + ", "
			+ "'" + obj.getFileName() + "', "
			+ "'" + obj.getDesc() + "', "
			+ obj.getFileSize() + ", "
			+ "'" + dtStr + "')";

		BoxStatic.logger.debug(sql);
		m_db.executeUpdate(sql);

		closeConnection();
	}

	public void remove() throws Exception {
		String sql = "delete from download_file where id = " + m_iID;
		makeConnection();
		m_db.executeUpdate(sql);
		closeConnection();
	}

	public Object findByKey() throws Exception {
		String sql = "select id from download_file where id = " + m_iID;
		makeConnection();
		m_db.executeQuery(sql);
		DownLoadKey pk = null;

		if(m_db.next()) {
			pk = new DownLoadKey(m_db.getInt(1));
		}
		closeConnection();
		return pk;
	}

	public Iterator findBySql(String sqlWhere) throws LinuxboxException {
		Vector v = null;

		try {
			String sql = "select id from download_file " + sqlWhere;
			BoxStatic.logger.debug(sql);
			makeConnection();
			m_db.executeQuery(sql);
			DownLoadKey pk = null;
			v = new Vector();

			while (m_db.next()) {
				pk = new DownLoadKey(m_db.getInt(1));
				v.addElement(pk);
			}
			closeConnection();
		} catch(Exception e) {
			throw new LinuxboxException(ExceptionHelper.DOWNLOAD, "DL0002", 
				"Can't find record: " + e.getMessage());
		}
		return v.iterator();
	}
}
